<template>
  <div class="coupon-page">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">🎫 礼券管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增礼券
          </el-button>
        </div>
      </template>

      <!-- 统计 -->
      <el-row :gutter="20" class="stat-overview">
        <el-col :span="6">
          <el-statistic title="礼券总数" :value="12345" />
        </el-col>
        <el-col :span="6">
          <el-statistic title="已使用" :value="8956" value-style="color: #67c23a" />
        </el-col>
        <el-col :span="6">
          <el-statistic title="未使用" :value="2389" value-style="color: #409eff" />
        </el-col>
        <el-col :span="6">
          <el-statistic title="已过期" :value="1000" value-style="color: #f56c6c" />
        </el-col>
      </el-row>

      <el-table :data="tableData" border stripe>
        <el-table-column prop="code" label="礼券编码" width="150" />
        <el-table-column prop="name" label="礼券名称" width="200" />
        <el-table-column prop="type" label="类型" width="120">
          <template #default="{ row }">
            {{ getTypeName(row.type) }}
          </template>
        </el-table-column>
        <el-table-column prop="value" label="面值/折扣" width="120">
          <template #default="{ row }">
            <el-tag type="danger">
              {{ row.type === 'cash' ? '¥' + row.value : row.value + '折' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="minAmount" label="使用门槛" width="120">
          <template #default="{ row }">
            {{ row.minAmount > 0 ? '满¥' + row.minAmount : '无门槛' }}
          </template>
        </el-table-column>
        <el-table-column prop="total" label="发放数量" width="100" />
        <el-table-column prop="used" label="已使用" width="100" />
        <el-table-column prop="validDays" label="有效期" width="100">
          <template #default="{ row }">
            {{ row.validDays }}天
          </template>
        </el-table-column>
        <el-table-column prop="enabled" label="状态" width="80">
          <template #default="{ row }">
            <el-switch v-model="row.enabled" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleIssue(row)">发放</el-button>
            <el-button link type="success" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="info" @click="handleView(row)">详情</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="700px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="礼券名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="礼券类型">
          <el-radio-group v-model="form.type">
            <el-radio label="cash">代金券</el-radio>
            <el-radio label="discount">折扣券</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="form.type === 'cash' ? '面值' : '折扣'">
          <el-input-number v-model="form.value" :min="0" :precision="form.type === 'cash' ? 2 : 1" />
          <span style="margin-left: 10px;">{{ form.type === 'cash' ? '元' : '折' }}</span>
        </el-form-item>
        <el-form-item label="使用门槛">
          <el-input-number v-model="form.minAmount" :min="0" :precision="2" />
          <span style="margin-left: 10px;">元（0表示无门槛）</span>
        </el-form-item>
        <el-form-item label="发放数量">
          <el-input-number v-model="form.total" :min="1" />
        </el-form-item>
        <el-form-item label="有效天数">
          <el-input-number v-model="form.validDays" :min="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const tableData = ref([
  { code: 'CPN001', name: '50元代金券', type: 'cash', value: 50, minAmount: 200, total: 1000, used: 678, validDays: 30, enabled: true },
  { code: 'CPN002', name: '8折优惠券', type: 'discount', value: 8, minAmount: 0, total: 2000, used: 1234, validDays: 15, enabled: true },
  { code: 'CPN003', name: '100元代金券', type: 'cash', value: 100, minAmount: 500, total: 500, used: 234, validDays: 60, enabled: true }
])

const dialogVisible = ref(false)
const dialogTitle = ref('新增礼券')

const form = reactive({
  name: '',
  type: 'cash',
  value: 0,
  minAmount: 0,
  total: 100,
  validDays: 30
})

const getTypeName = (type) => {
  return type === 'cash' ? '代金券' : '折扣券'
}

const handleAdd = () => {
  dialogTitle.value = '新增礼券'
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑礼券'
  Object.assign(form, row)
  dialogVisible.value = true
}

const handleIssue = () => {
  ElMessage.info('发放礼券')
}

const handleView = () => {
  ElMessage.info('查看详情')
}

const handleDelete = () => {
  ElMessage.success('删除成功')
}

const handleSubmit = () => {
  ElMessage.success('保存成功')
  dialogVisible.value = false
}
</script>

<style scoped>
.coupon-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.stat-overview {
  margin-bottom: 20px;
  padding: 20px;
  background: white;
  border-radius: 8px;
}
</style>

